package com.bangcommunity.bbframe.sdm.web.shiro.token;

import com.bangcommunity.bbframe.sdm.web.shiro.ISdmPemissionService;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * @author tanghc
 * @Date 17/11/16
 */
public class SdmTokenAuthFilter<PK extends Serializable> extends AbstractTokenAuthFilter {
    @Autowired(required = false)
    private ISdmPemissionService sdmPemissionService;
    SdmTokenAuthenticationFilter sdmTokenAuthenticationFilter;
    SdmTokenAuthorizeFilter sdmTokenAuthorizeFilter;
    @Override
    protected boolean hasPermition(ServletRequest request, ServletResponse response, String resourceId) throws Exception {
        return getSdmTokenAuthorizeFilter().hasPermition(request, response, resourceId);
    }

    @Override
    protected String getResourceId(ServletRequest request, ServletResponse response) {
        return getSdmTokenAuthorizeFilter().getResourceId(request, response);
    }

    @Override
    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
        return getSdmTokenAuthenticationFilter().createToken(request, response);
    }
    @Override
    public boolean isForceRest(){
        return  sdmPemissionService.forceRest();
    }

    private SdmTokenAuthenticationFilter getSdmTokenAuthenticationFilter(){
        if(null==sdmTokenAuthenticationFilter){
            sdmTokenAuthenticationFilter = new SdmTokenAuthenticationFilter();
        }
        return sdmTokenAuthenticationFilter;

    }
    private SdmTokenAuthorizeFilter getSdmTokenAuthorizeFilter(){
        if(null==sdmTokenAuthorizeFilter){
            sdmTokenAuthorizeFilter = new SdmTokenAuthorizeFilter();
            sdmTokenAuthorizeFilter.setSdmPemissionService(sdmPemissionService);
        }
        return sdmTokenAuthorizeFilter;
    }

}
